package com.huawei.esdk.tp.device.smc;

import java.util.Date;
import java.util.List;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.platform.commu.itf.ICXFSOAPProtocolAdapter;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.cxf.enumgen.ContinuousPresenceMode;
import com.huawei.esdk.tp.cxf.enumgen.ContinuousPresenceModeEXMapping;
import com.huawei.esdk.tp.device.smc.convert.TPConfCtrConvert;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.bean.ContinuousPresenceCapability;
import com.huawei.esdk.tp.domain.model.bean.ContinuousPresenceParam;
import com.huawei.esdk.tp.southcommu.cxf.gen.AddSiteInScheduledConfResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.AddSiteInScheduledConfSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ArrayOfStringSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.AudioSwitchParamSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ConferenceIdSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ContinuousPresenceParamSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DelSiteInScheduledConfResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DelSiteInScheduledConfSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DeleteScheduledConfResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DeleteScheduledConfSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.GetContinuousPresenceInfoResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.GetContinuousPresenceInfoSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.MuteSitesResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.MuteSitesSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuietSitesResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuietSitesSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SetAudioSwitchResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SetAudioSwitchSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SetBroadcastResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SetBroadcastSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SetContinuousPresenceResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SetContinuousPresenceSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SetVideoSourceResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SetVideoSourceSouth;

public class ConferenceControlCapabilityImpl extends V1R1C01BaseCapability implements IConferenceControlCapability
{
    
    public ConferenceControlCapabilityImpl(ICXFSOAPProtocolAdapter cxfSOAPProtocolAdapter)
    {
        super(cxfSOAPProtocolAdapter);
    }
    
    private TPConfCtrConvert tPConfCtrConvertService = ApplicationContextUtil.getBean("tPConfCtrConvert");
    
    /**
     * 删除会议 * @see
     * com.huawei.esdk.tp.device.smc.IConferenceControlServer#endConf(java
     * .lang.String)
     */
    @Override
    public SDKErrorCode deleteConf(String confid, Date beginTime)
    {
        SDKErrorCode code = new SDKErrorCode();
        ConferenceIdSouth conferenceId = new ConferenceIdSouth();
        conferenceId.setConfInternalId(Long.parseLong(confid));
        DeleteScheduledConfSouth south = new DeleteScheduledConfSouth();
        south.setConfId(conferenceId);
        if (null != beginTime)
        {
            south.setBeginTime(DateUtils.setTimezoneToUTC(DateUtils.toGregorianCalendarDate(beginTime), 0));
        }
        
        DeleteScheduledConfResponseSouth response;
        try
        {
            response = (DeleteScheduledConfResponseSouth)sendProtocolMessage(south, "deleteScheduledConf");
        }
        catch (ProtocolAdapterException e)
        {
            code.setErrCode(e.getErrorCode());
            return code;
        }
        code.setErrCode(response.getDeleteScheduledConfResult().getErrCode());
        code.setSubSystem(String.valueOf(response.getDeleteScheduledConfResult().getSubSystem()));
        code.setDescription(response.getDeleteScheduledConfResult().getDescription());
        return code;
    }
    
    /**
     * 向会议中添加会场 * @see
     * com.huawei.esdk.tp.device.smc.IConferenceControlCapability#addSiteToConf
     * (java.lang.String, java.lang.String)
     */
    @Override
    public SDKErrorCode addSiteToConf(String confId, Site site)
    {
        SDKErrorCode result = new SDKErrorCode();
        // 生成协议相关的bean
        AddSiteInScheduledConfSouth southReq = tPConfCtrConvertService.addSiteToConf2soap(confId, site);
        
        AddSiteInScheduledConfResponseSouth response = null;
        try
        {
            response = (AddSiteInScheduledConfResponseSouth)sendProtocolMessage(southReq, "addSiteInScheduledConf");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        if (null == response)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(response.getAddSiteInScheduledConfResult().getErrCode());
        }
        
        return result;
    }
    
    @Override
    public SDKErrorCode setAudioSwitch(String confId, boolean onOff)
    {
        SetAudioSwitchSouth reqBean = new SetAudioSwitchSouth();
        SDKErrorCode result = new SDKErrorCode();
        
        ConferenceIdSouth conferenceId = new ConferenceIdSouth();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceId);
        reqBean.setEnable(onOff);
        
        SetAudioSwitchResponseSouth res = null;
        try
        {
            res = (SetAudioSwitchResponseSouth)sendProtocolMessage(reqBean, "setAudioSwitch");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        if (null == res)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(res.getSetAudioSwitchResult().getErrCode());
        }
        return result;
    }
    
    @Override
    public SDKErrorCode delSiteFromConf(String confId, String siteUri)
    {
        DelSiteInScheduledConfSouth reqBean = new DelSiteInScheduledConfSouth();
        ConferenceIdSouth conferenceId = new ConferenceIdSouth();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceId);
        reqBean.setSiteUri(siteUri);
        
        DelSiteInScheduledConfResponseSouth res = null;
        SDKErrorCode result = new SDKErrorCode();
        try
        {
            res = (DelSiteInScheduledConfResponseSouth)sendProtocolMessage(reqBean, "delSiteInScheduledConf");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        if (null == res)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(res.getDelSiteInScheduledConfResult().getErrCode());
        }
        return result;
    }
    
    @Override
    public SDKErrorCode setAudioSwitchEX(String confId, boolean enable, Integer swtichGate)
    {
        SetAudioSwitchSouth reqBean = new SetAudioSwitchSouth();
        
        ConferenceIdSouth conferenceId = new ConferenceIdSouth();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceId);
        
        AudioSwitchParamSouth audioSwitchParam = new AudioSwitchParamSouth();
        audioSwitchParam.setSwtichGate(swtichGate);
        
        reqBean.setEnable(enable);
        
        SetAudioSwitchResponseSouth res = null;
        SDKErrorCode result = new SDKErrorCode();
        try
        {
            res = (SetAudioSwitchResponseSouth)sendProtocolMessage(reqBean, "setAudioSwitch");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        if (null == res)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(res.getSetAudioSwitchResult().getErrCode());
        }
        return result;
    }
    
    @Override
    public SDKErrorCode setBroadcastSiteEx(String confId, String siteUri)
    {
        SetBroadcastSouth reqBean = new SetBroadcastSouth();
        ConferenceIdSouth conferenceId = new ConferenceIdSouth();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceId);
        reqBean.setSiteUri(siteUri);
        // reqBean.e
        
        SetBroadcastResponseSouth res = null;
        SDKErrorCode result = new SDKErrorCode();
        try
        {
            res = (SetBroadcastResponseSouth)sendProtocolMessage(reqBean, "setBroadcast");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        if (null == res)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(res.getSetBroadcastResult().getErrCode());
        }
        
        return result;
    }
    
    @Override
    public SDKErrorCode setSitesMuteEx(String confId, List<String> siteUris, boolean enable)
    {
        MuteSitesSouth reqBean = new MuteSitesSouth();
        ConferenceIdSouth conferenceId = new ConferenceIdSouth();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceId);
        
        reqBean.setIsMute(enable);
        
        ArrayOfStringSouth arrayOfStringSouth = new ArrayOfStringSouth();
        
        for (String stUri : siteUris)
        {
            arrayOfStringSouth.getString().add(stUri);
        }
        
        reqBean.setSiteUris(arrayOfStringSouth);
        
        MuteSitesResponseSouth res = null;
        SDKErrorCode result = new SDKErrorCode();
        try
        {
            res = (MuteSitesResponseSouth)sendProtocolMessage(reqBean, "muteSites");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        if (null == res)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(res.getMuteSitesResult().getErrCode());
        }
        
        return result;
    }
    
    @Override
    public SDKErrorCode setSitesQuietEx(String confId, List<String> siteUris, boolean enable)
    {
        QuietSitesSouth reqBean = new QuietSitesSouth();
        ConferenceIdSouth conferenceId = new ConferenceIdSouth();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceId);
        
        reqBean.setIsQuiet(enable);
        
        ArrayOfStringSouth arrayOfStringSouth = new ArrayOfStringSouth();
        
        for (String stUri : siteUris)
        {
            arrayOfStringSouth.getString().add(stUri);
        }
        
        reqBean.setSiteUris(arrayOfStringSouth);
        
        QuietSitesResponseSouth res = null;
        SDKErrorCode result = new SDKErrorCode();
        try
        {
            res = (QuietSitesResponseSouth)sendProtocolMessage(reqBean, "quietSites");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        if (null == res)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(res.getQuietSitesResult().getErrCode());
        }
        
        return result;
    }
    
    @Override
    public SDKErrorCode setBroadcastContinuousPresenceEx(String confId)
    {
        SetBroadcastSouth reqBean = new SetBroadcastSouth();
        ConferenceIdSouth conferenceId = new ConferenceIdSouth();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceId);
        
        SetBroadcastResponseSouth res = null;
        SDKErrorCode result = new SDKErrorCode();
        try
        {
            res = (SetBroadcastResponseSouth)sendProtocolMessage(reqBean, "setBroadcast");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        if (null == res)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(res.getSetBroadcastResult().getErrCode());
        }
        
        return result;
        
    }
    
    @Override
    public SDKErrorCode setContinuousPresenceEx(String confId, ContinuousPresenceParam continuousPresenceParam)
    {
        
        SetContinuousPresenceSouth reqBean = new SetContinuousPresenceSouth();
        ConferenceIdSouth conferenceId = new ConferenceIdSouth();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceId);
        
        ContinuousPresenceParamSouth presenceParam = new ContinuousPresenceParamSouth();
        
        ArrayOfStringSouth arrayOfString = null;
        if (null != continuousPresenceParam.getSubPics() && continuousPresenceParam.getSubPics().size() > 0)
        {
            arrayOfString = new ArrayOfStringSouth();
            for (String pic : continuousPresenceParam.getSubPics())
            {
                arrayOfString.getString().add(pic);
            }
        }
        presenceParam.setSubPics(arrayOfString);
        
        ContinuousPresenceMode presenceMode =
            ContinuousPresenceModeEXMapping.getInstance().int2Enum(continuousPresenceParam.getPresenceMode());
        
        presenceParam.setMode(presenceMode);
        
        reqBean.setCpParam(presenceParam);
        
        // R5C00，不再需要判断会场是否存在
//        reqBean.setTarget(this.tPConfCtrConvertService.continuousPresenceTargetMappingConvert(continuousPresenceParam.getTarget(),
//            continuousPresenceParam.isTargetIsSiteUri()));
        reqBean.setTarget(this.tPConfCtrConvertService.continuousPresenceTargetMappingConvert(continuousPresenceParam.getTarget()));
        
        SetContinuousPresenceResponseSouth res = null;
        SDKErrorCode result = new SDKErrorCode();
        try
        {
            res = (SetContinuousPresenceResponseSouth)sendProtocolMessage(reqBean, "setContinuousPresence");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        if (null == res)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(res.getSetContinuousPresenceResult().getErrCode());
        }
        
        return result;
    }
    
    @Override
    public SDKErrorCode setVideoSource(String confId, String siteUri, String videoSrcSiteUri, boolean isLock)
    {
        SetVideoSourceSouth reqBean = new SetVideoSourceSouth();
        ConferenceIdSouth conferenceIdSouth = new ConferenceIdSouth();
        conferenceIdSouth.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceIdSouth);
        reqBean.setSiteUri(siteUri);
        reqBean.setVideoSourceUri(videoSrcSiteUri);
        reqBean.setIsLock(isLock);
        
        SetVideoSourceResponseSouth res = null;
        SDKErrorCode errorCode = new SDKErrorCode();
        try
        {
            res = (SetVideoSourceResponseSouth)sendProtocolMessage(reqBean, "setVideoSource");
        }
        catch (ProtocolAdapterException e)
        {
            errorCode.setErrCode(e.getErrorCode());
            return errorCode;
        }
        
        if (null == res)
        {
            errorCode.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            errorCode.setErrCode(res.getSetVideoSourceResult().getErrCode());
        }
        
        return errorCode;
        
    }
    
    public SDKResult<ContinuousPresenceCapability> getContinuousPresenceInfoEx(String confId)
    {
        GetContinuousPresenceInfoSouth reqBean = new GetContinuousPresenceInfoSouth();
        ConferenceIdSouth conferenceId = new ConferenceIdSouth();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceId);
        
        GetContinuousPresenceInfoResponseSouth res = null;
        SDKResult<ContinuousPresenceCapability> result = new SDKResult<ContinuousPresenceCapability>();
        try
        {
            res = (GetContinuousPresenceInfoResponseSouth)sendProtocolMessage(reqBean, "getContinuousPresenceInfo");
            result = tPConfCtrConvertService.getContinuousPresenceInfoCommSoap2model(res);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        return result;
    }
}
